/**
 * 文件名：****.java
 * 版权：Copyright Lightkit All Rights Reserved.
 * 描述：*****
 */
package com.lightkits.wms.domain.allotmanagementpc.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.lightkits.common.db.entity.FndEventTypeBase;
import com.lightkits.common.db.entity.WmRequestHeader;
import com.lightkits.common.db.entity.event.BdEvent;
import com.lightkits.framework.soa.client.id.GetGlobalIDRequestType;
import com.lightkits.framework.soa.client.id.GetGlobalIDResponseType;
import com.lightkits.framework.soa.client.id.IDServiceClient;
import com.lightkits.wms.domain.allotmanagementpc.CreateDocumentEvetParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Iterator;

/**
 * @author 魏静
 * @create 2019-07-22 16:17
 * @desc 创建单据事件
 **/
@Service
public class CreateDocumentEvetParamServiceImpl implements CreateDocumentEvetParamService {
    private final IDServiceClient idAdapterService;

    @Autowired
    public CreateDocumentEvetParamServiceImpl(IDServiceClient idAdapterService) {
        this.idAdapterService = idAdapterService;
    }

    /**
     * 创建事件
     *
     * @param params        领料单头参数
     * @param eventTypeCode 事件类型参数
     * @return BdEvent
     */
    @Override
    public BdEvent createEvent(WmRequestHeader params, String eventTypeCode) {
        GetGlobalIDRequestType idRequest = new GetGlobalIDRequestType();
        idRequest.setCount(1);
        GetGlobalIDResponseType idResponse = idAdapterService.getGlobalId(idRequest);
        Iterator<Long> sfIds = idResponse.getIds().iterator();
        BdEvent event = new BdEvent();
        FndEventTypeBase eventTypeBase = new FndEventTypeBase()
                .selectOne(new EntityWrapper<FndEventTypeBase>()
                        .eq(FndEventTypeBase.EVENT_TYPE_CODE, eventTypeCode));

        event.setEventTypeId(eventTypeBase.getId());
        event.setEventTypeCode(eventTypeBase.getEventTypeCode());
        event.setEventRequestId(sfIds.next());
        event.setRemark(params.getRemark());
        event.setWorkerId(params.getCreatorId());
        event.setOrganizationId(params.getOrganizationId());


        event.setCalendarDay(new Date());
        return event;
    }
}
